package com.team8.system.service.service;

import com.team8.common.results.Result;
import com.team8.system.pojo.Person;
import com.team8.system.service.dao.PersonDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

@Service
public class PersonService {

    @Autowired
    private PersonDao personDao;


    public Result addPerson(Person person) {

        Example example = new Example(Person.class);
        Example.Criteria criteria = example.createCriteria();

        criteria.andEqualTo("idCard", person.getIdCard());
        criteria.andEqualTo("userId", person.getUserId());

        if(!CollectionUtils.isEmpty(personDao.selectByExample(example))){
            return new Result(400, "此用户已经添加过了!");
        }

        int insert = personDao.insert(person);

        return Result.getSuccessInstance();

    }

    public List<Person> getUserPersons(Integer userId) {

        List<Person> persons = personDao.getUserPersons(userId);

        return persons;
    }
}
